<template>
	<view class="text-white padding-lr">
		<!-- #ifdef MP-WEIXIN -->
		<view class="flex  padding-tb" @click="bindMsg">
			<view class="flex-sub text-df" style="line-height: 50upx;">订阅消息</view>
			<view class="jia">
				<u-icon name="plus" color="#FFFFFF"></u-icon>
			</view>
		</view>
		<!-- #endif -->
		<view class="flex  padding-tb" @click="goNav('/pages/public/pwd')">
			<view class="flex-sub text-df" style="line-height: 50upx;">修改密码</view>
			<image src="../../static/images/my/right.png" style="line-height: 50upx;width: 15rpx;height: 30rpx;">
			</image>
		</view>
		<view class="flex  padding-tb" @click="goNav('/my/setting/zhuxiao')">
			<view class="flex-sub text-df" style="line-height: 50upx;">注销账号</view>
			<image src="../../static/images/my/right.png" style="line-height: 50upx;width: 15rpx;height: 30rpx;">
			</image>
		</view>
		<view class="flex  padding-tb" @click="goNav('/my/setting/xieyi')">
			<view class="flex-sub text-df" style="line-height: 50upx;">用户协议</view>
			<image src="../../static/images/my/right.png" style="line-height: 50upx;width: 15rpx;height: 30rpx;">
			</image>
		</view>
		<view class="flex  padding-tb" @click="goNav('/my/setting/mimi')">
			<view class="flex-sub text-df" style="line-height: 50upx;">隐私政策</view>
			<image src="../../static/images/my/right.png" style="line-height: 50upx;width: 15rpx;height: 30rpx;">
			</image>
		</view>
		<view class="flex  padding-tb" @click="goNav('/my/setting/about')">
			<view class="flex-sub text-df" style="line-height: 50upx;">关于我们</view>
			<image src="../../static/images/my/right.png" style="line-height: 50upx;width: 15rpx;height: 30rpx;">
			</image>
		</view>
		<view class="btn" @click="goOut">退出登录</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				arr: [],
				showModal: true,
			}
		},
		onLoad() {
			this.$Request.getT('/app/common/type/235').then(res => { //新订单通知
				if (res.code == 0) {
					if (res.data && res.data.value) {
						this.arr.push(res.data.value)
					}
				}
			})
			this.$Request.getT('/app/common/type/311').then(res => { //商家私聊消息通知
				if (res.code == 0) {
					if (res.data && res.data.value) {
						this.arr.push(res.data.value)
					}
				}
				
			})
			// 商家端订单状态通知	623
			this.$Request.getT('/app/common/type/623').then(res => { //商家端订单状态通知
				if (res.code == 0) {
					if (res.data && res.data.value) {
						this.arr.push(res.data.value)
					}
				}
				
			})
			
			// #ifdef MP-WEIXIN
			//订阅
			if (this.showModal) {
				this.openMsg()
			}
			// #endif
		},
		methods: {
			// 开启订阅消息
			openMsg() {
				console.log('订阅消息')
				var that = this
				uni.getSetting({
					withSubscriptions: true, //是否获取用户订阅消息的订阅状态，默认false不返回
					success(ret) {
						console.log(ret.subscriptionsSetting, '------------------')
						// if (ret.subscriptionsSetting.itemSettings && Object.keys(ret.subscriptionsSetting.itemSettings).length == 2) {
						if (ret.subscriptionsSetting.itemSettings) {
							uni.setStorageSync('sendMsg', true)
							uni.openSetting({ // 打开设置页 
								success(rea) {
									console.log(rea.authSetting)
								}
							});
						} else { // 用户没有点击“总是保持以上，不再询问”则每次都会调起订阅消息
							console.log(99999)
							uni.setStorageSync('sendMsg', false)
							uni.showModal({
								title: '提示',
								content: '为了更好的体验,请绑定消息推送',
								confirmText: '确定',
								cancelText: '取消',
								success: function(res) {
									if (res.confirm) {
										wx.requestSubscribeMessage({
											tmplIds: that.arr,
											success(re) {
												console.log(JSON.stringify(re),
													'++++++++++++++')
												var datas = JSON.stringify(re);
												if (datas.indexOf("accept") != -1) {
													console.log(re)
													// uni.setStorageSync('sendMsg', true)
												}
											},
											fail: (res) => {
												console.log(res)
											}
										})
										// uni.setStorageSync('sendMsg', true)
										console.log('确认')
										that.showModal = false
									} else if (res.cancel) {
										console.log('取消')
										// uni.setStorageSync('sendMsg', false)
										that.showModal = true
									}
								}
							})
						}
					}
				})
			},
			bindMsg() {
				// #ifdef MP-WEIXIN
				if (uni.getStorageSync('sendMsg')) {
					uni.requestSubscribeMessage({
						tmplIds: this.arr,
						success(re) {
							// console.log(re,'**********')
							var datas = JSON.stringify(re);
							if (datas.indexOf("accept") != -1) {
								console.log(re)
								uni.showToast({
									title: '订阅成功',
									icon: 'none'
								})
							}
						},
						fail: (res) => {
							console.log(res)
						}
					})
				}
								
				// #endif
			},
			goNav(e) {
				uni.navigateTo({
					url: e
				})
			},
			goOut() {
				uni.showModal({
					title: '提示',
					content: '确定退出登录吗？',
					success: function(res) {
						if (res.confirm) {
							console.log('用户点击确定');
							uni.removeStorageSync('userName')
							uni.removeStorageSync('avatar')
							uni.removeStorageSync('userId')
							uni.removeStorageSync('token')
							uni.removeStorageSync('phone')
							uni.removeStorageSync('zhiFuBaoName')
							uni.removeStorageSync('zhiFuBao')
							uni.removeStorageSync('invitationCode')
							uni.removeStorageSync('unionId')
							uni.removeStorageSync('openId')
							uni.removeStorageSync('isVIP')
							uni.showToast({
								title: '退出成功！',
								icon: 'none'
							})
							setTimeout(function() {
								uni.navigateBack()
							}, 1000)
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			}
		}

	}
</script>

<style>
	.btn {
		width: 100%;
		height: 80upx;
		background: #557EFD;
		border-radius: 6upx;
		text-align: center;
		line-height: 80upx;
		margin-top: 40upx;
		font-size: 34upx;
		color: #fff;
	}

	.jia {
		background: #346EF6;
		border-radius: 50%;
		width: 55rpx;
		height: 55rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
</style>